package cn.micro.system.manage.controller;



import cn.micro.system.manage.annotate.UniqueRole;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimRole;
import cn.micro.system.manage.entity.vo.AssginRoleVo;
import cn.micro.system.manage.entity.vo.SysRoleQueryVo;
import cn.micro.system.manage.service.SysDimRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/manage/sysDimRole")
@Validated
@RequiredArgsConstructor
public class SysDimRoleController {

    private final SysDimRoleService sysDimRoleService;


    @ApiOperation("获取用户的角色数据")
    @GetMapping("toAssign/{userId}")
    @PreAuthorize("hasAnyAuthority('bnt.sysUser.list','bnt.sysRole.list')")
    public Result toAssign(@PathVariable String userId) {
        Map<String,Object> roleMap = sysDimRoleService.getRolesByUserId(userId);
        return Result.success(roleMap);
    }

    @ApiOperation("用户分配角色")
    @PostMapping("doAssign")
    @Log(title = "角色管理--用户分配角色", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysDimRoleService.doAssign(assginRoleVo);
        return Result.success();
    }

    //7 批量删除
    // 多个id值 [1,2,3]
    // json数组格式 --- java的list集合
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    @Log(title = "角色管理--批量删除", businessType = BusinessType.DELETE)
    public Result batchRemove(List<Long> ids) {
        sysDimRoleService.removeByIds(ids);
        return Result.success();
    }

    //6 修改-最终修改
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("最终修改")
    @PutMapping("edit")
    @Log(title = "角色管理--修改角色", businessType = BusinessType.UPDATE)
    public Result edit(@Validated(value = ValidateGroup.edit.class) @UniqueRole @RequestBody SysDimRole sysRole) {
        boolean isSuccess = sysDimRoleService.updateById(sysRole);
        if(isSuccess) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //5 修改-根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("findRoleById/{id}")
    public Result findRoleById(@PathVariable Long id) {
        SysDimRole sysRole = sysDimRoleService.getById(id);
        return Result.success(sysRole);
    }

    //4 添加
    // @RequestBody 不能使用get提交方式
    // 传递json格式数据，把json格式数据封装到对象里面 {...}
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @Log(title = "角色管理--添加角色",businessType = BusinessType.INSERT)
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result saveRole(@Validated(value = ValidateGroup.add.class) @UniqueRole @RequestBody SysDimRole sysRole) {
        boolean isSuccess = sysDimRoleService.save(sysRole);
        if(isSuccess) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //3 条件分页查询
    // page当前页  limit每页记录数
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{size}")
    public Result findPageQueryRole(@PathVariable int page,
                                    @PathVariable int size,
                                    SysRoleQueryVo sysRoleQueryVo) {
        return Result.success(new PageInfo<>(sysDimRoleService.selectPage(page,size,sysRoleQueryVo)));
    }

    //2 逻辑删除接口
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("逻辑删除接口")
    @DeleteMapping("remove/{id}")
    @Log(title = "角色管理--删除角色信息", businessType = BusinessType.DELETE)
    public Result removeRole(@PathVariable Long id) {
        //调用方法删除
        boolean isSuccess = sysDimRoleService.removeById(id);
        if(isSuccess) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //1 查询所有记录
    @ApiOperation("查询所有记录")
    @GetMapping("findAll")
    @PreAuthorize("hasAnyAuthority('bnt.sysUser.list','bnt.sysRole.list')")
    public Result findAllRole() {
        //调用service
        List<SysDimRole> list = sysDimRoleService.list();
        return Result.success(list);
    }

}
